home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / tcop / utility.c < prev   
Encoding:
C/C++ Source or Header  |  1992-08-27  |  10.9 KB  |  515 lines

  1. /* ----------------------------------------------------------------
  2.  *   FILE
  3.  *    utility.c
  4.  *    
  5.  *   DESCRIPTION
  6.  *         Contains functions which control the execution of the
  7.  *         POSTGRES utility commands.  At one time acted as an
  8.  *    interface between the Lisp and C systems.
  9.  *    
  10.  *   INTERFACE ROUTINES
  11.  *    
  12.  *   NOTES
  13.  *    
  14.  *   IDENTIFICATION
  15.  *    $Header: /private/postgres/src/tcop/RCS/utility.c,v 1.65 1992/06/28 03:48:22 mao Exp $
  16.  * ----------------------------------------------------------------
  17.  */
  18.  
  19. #include "tmp/postgres.h"
  20.  
  21.  RcsId("$Header: /private/postgres/src/tcop/RCS/utility.c,v 1.65 1992/06/28 03:48:22 mao Exp $");
  22.  
  23. /* ----------------
  24.  *    FILE INCLUDE ORDER GUIDELINES
  25.  *
  26.  *    1) tcopdebug.h
  27.  *    2) various support files ("everything else")
  28.  *    3) node files
  29.  *    4) catalog/ files
  30.  *    5) execdefs.h and execmisc.h, if necessary.
  31.  *    6) extern files come last.
  32.  * ----------------
  33.  */
  34. #include "tcop/tcopdebug.h"
  35.  
  36. #include "parser/parse.h"
  37. #include "parser/parsetree.h"
  38. #include "utils/log.h"
  39.  
  40. #include "nodes/pg_lisp.h"
  41. #include "tcop/dest.h"
  42.  
  43. #include "tmp/globals.h"
  44.  
  45. /* ----------------
  46.  *    CHECK_IF_ABORTED() is used to avoid doing unnecessary
  47.  *    processing within an aborted transaction block.
  48.  * ----------------
  49.  */
  50. #define CHECK_IF_ABORTED() \
  51. if (IsAbortedTransactionBlockState()) { \
  52.     elog(NOTICE, "(transaction aborted): %s", \
  53.      "queries ignored until END"); \
  54.     commandTag = "*ABORT STATE*"; \
  55.     break; \
  56. } \
  57.         
  58. /* ----------------
  59.  *    general utility function invoker
  60.  * ----------------
  61.  */
  62. void
  63. ProcessUtility(command, args, commandString, dest)
  64.     int        command;    /* "tag" */
  65.     LispValue    args;
  66.     char     *commandString;
  67.     CommandDest dest;
  68. {
  69.     String    commandTag = NULL;
  70.     
  71.     switch (command) {
  72.     /* ********************************
  73.      *    transactions
  74.      * ********************************
  75.      */
  76.     case BEGIN_TRANS:
  77.     commandTag = "BEGIN";
  78.     CHECK_IF_ABORTED();
  79.     
  80.     BeginTransactionBlock();
  81.     break;
  82.     
  83.     case END_TRANS:
  84.     commandTag = "END";
  85.     EndTransactionBlock();
  86.     break;
  87.     
  88.     case ABORT_TRANS:
  89.     commandTag = "ABORT";
  90.     UserAbortTransactionBlock();
  91.     break;
  92.     
  93.     /* ********************************
  94.      *    portal manipulation
  95.      * ********************************
  96.      */
  97.     case CLOSE:
  98.     commandTag = "CLOSE";
  99.     CHECK_IF_ABORTED();
  100.     
  101.     PerformPortalClose((null(CAR(args))) ? NULL : CString(CAR(args)),
  102.                dest);
  103.     break;
  104.     
  105.     case FETCH:
  106.     commandTag = "FETCH";
  107.     CHECK_IF_ABORTED();
  108.     
  109.     {
  110.         String    portalName = NULL;
  111.         bool    forward;
  112.         int    count;
  113.         
  114.         if (!null(CAR(args))) {
  115.         portalName = CString(CAR(args));
  116.         }
  117.         forward = (bool)(CAtom(CADR(args)) == FORWARD);
  118.         count = 0;
  119.         if (!lispAtomp(CADDR(args))) {
  120.         count = CInteger(CADDR(args));
  121.         
  122.         if (count < 0) {
  123.             elog(WARN, "Fetch: specify nonnegative count");
  124.         }
  125.         if (count == 0) {
  126.             break;
  127.         }
  128.         }
  129.         PerformPortalFetch(portalName, forward, count, commandTag, dest);
  130.     }
  131.     break;
  132.     
  133.     case MOVE:
  134.     commandTag = "MOVE";
  135.     CHECK_IF_ABORTED();
  136.     
  137.     elog(NOTICE, "MOVE: currently unimplemented");
  138.     break;
  139.     
  140.     /* ********************************
  141.      *    relation and attribute manipulation
  142.      * ********************************
  143.      */
  144.     case CREATE:
  145.     commandTag = "CREATE";
  146.     CHECK_IF_ABORTED();
  147.     
  148.     DefineRelation(CString(CAR(args)),    /*  relation name */
  149.                CADR(args),        /*  parameters */
  150.                CDR(CDR(args)));    /*  schema */
  151.     break;
  152.     
  153.     case DESTROY:
  154.     commandTag = "DESTROY";
  155.     CHECK_IF_ABORTED();
  156.     
  157.     {
  158.         LispValue relationName;
  159.         foreach (relationName, args) {
  160.         RemoveRelation(CString(CAR(relationName)));
  161.         }
  162.     }
  163.     break;
  164.     
  165.     case PURGE:
  166.     commandTag = "PURGE";
  167.     CHECK_IF_ABORTED();
  168.     
  169.     {
  170.         List    tags;
  171.         String    beforeString = NULL;    /* absolute time string */
  172.         String    afterString = NULL;    /* relative time string */
  173.         
  174.         tags = CAR(CADR(args));
  175.         switch (length(tags)) {
  176.         case 1:
  177.         break;
  178.         case 2:
  179.         if (CInteger(CAR(tags)) == BEFORE) {
  180.             beforeString = CString(CADR(tags));
  181.         } else {
  182.             afterString = CString(CADR(tags));
  183.         }
  184.         break;
  185.         case 3:
  186.         beforeString = CString(CADR(tags));
  187.         afterString = CString(CADR(tags));
  188.         break;
  189.         }
  190.         RelationPurge(CString(CAR(args)), beforeString, afterString);
  191.     }
  192.     break;
  193.     
  194.     case COPY:
  195.     commandTag = "COPY";
  196.     CHECK_IF_ABORTED();
  197.     
  198.     {
  199.         String    relationName;
  200.         String    fileName;
  201.         String    mapName = NULL;
  202.         bool    isBinary;
  203.         bool    noNulls;
  204.         bool    isFrom;
  205.             bool        pipe;
  206.  
  207.         relationName = CString(CAAR(args));
  208.         isBinary = (bool)!null(CADR(CAR(args)));
  209.         noNulls = (bool)!null(CADDR(CAR(args)));
  210.         /*
  211.          * discard '("relname" [BINARY] [NONULLS])
  212.          */
  213.         args = CDR(args);
  214.         
  215.         isFrom = (bool)(CAtom(CAAR(args)) == FROM);
  216.         fileName = CString(CADR(CAR(args)));
  217.  
  218.  
  219.         /* Free up file descriptors - going to do a read... */
  220.  
  221.         closeOneVfd();
  222.  
  223.             if (isFrom && !strcmp(fileName, "stdin"))
  224.             {
  225.                 pipe = true;
  226.             }
  227.             else if (!isFrom && !strcmp(fileName, "stdout"))
  228.             {
  229.                 pipe = true;
  230.             }
  231.  
  232.             /*
  233.              * Insist on a full pathname
  234.              */
  235.  
  236.             else if (*fileName == '/')
  237.             {
  238.                 pipe = false;
  239.             }
  240.             else
  241.             {
  242.                 elog(WARN, "COPY: full pathname required for filename");
  243.             }
  244.  
  245.         /*
  246.          * discard '(FROM/TO "filename")
  247.          */
  248.         args = CDR(args);
  249.  
  250.         if (pipe && IsUnderPostmaster) dest = CopyEnd;
  251.  
  252.         DoCopy(relationName, isBinary, isFrom, pipe, fileName);
  253.     }
  254.     break;
  255.     
  256.     case ADD_ATTR:
  257.     commandTag = "ADD";
  258.     CHECK_IF_ABORTED();
  259.     
  260.     PerformAddAttribute(CString(CAR(args)), CDR(args));
  261.     break;
  262.     
  263.     /*
  264.      * schema
  265.      */
  266.     case RENAME:
  267.     commandTag = "RENAME";
  268.     CHECK_IF_ABORTED();
  269.     
  270.     {
  271.         int    len;
  272.         len = length(args);
  273.         
  274.         /*
  275.          * skip unused "RELATION" or "ATTRIBUTE" tag
  276.          */
  277.         args = CDR(args);
  278.         
  279.         /* ----------------
  280.          *    XXX using len == 3 to tell the difference
  281.          *        between "rename rel to newrel" and
  282.          *        "rename att in rel to newatt" will not
  283.          *        work soon because "rename type/operator/rule"
  284.          *        stuff is being added. - cim 10/24/90
  285.          * ----------------
  286.          */
  287.         if (len == 3) {
  288.         /* ----------------
  289.          *    rename relation
  290.          *
  291.          *    Note: we also rename the "type" tuple
  292.          *    corresponding to the relation.
  293.          * ----------------
  294.          */
  295.         renamerel(CString(CAR(args)),      /* old name */
  296.               CString(CADR(args)));     /* new name */
  297.         TypeRename(CString(CAR(args)),     /* old name */
  298.                CString(CADR(args))); /* new name */
  299.         } else {
  300.         /* ----------------
  301.          *    rename attribute
  302.          * ----------------
  303.          */
  304.         renameatt(CString(CAR(args)),      /* relname */
  305.               CString(CADR(args)),      /* old att name */
  306.               CString(CADDR(args))); /* new att name */
  307.         }
  308.     }
  309.     break;
  310.     
  311.     /* ********************************
  312.      *    object creation / destruction
  313.      * ********************************
  314.      */
  315.     case DEFINE:
  316.     commandTag = "DEFINE";
  317.     CHECK_IF_ABORTED();
  318.     
  319.     switch(LISPVALUE_INTEGER(CAR(args))) {
  320.     case INDEX:    /* XXX no support for ARCHIVE indices, yet */
  321.         args = CDR(args);    /* skip "INDEX" token */
  322.         
  323.         DefineIndex(CString(CAR(args)), /* relation name */
  324.             CString(CADR(args)),    /* index name */
  325.             CString(CADDR(args)),    /* am name */
  326.             CADDR(CDR(args)),    /* parameters */
  327.             CADDR(CDR(CDR(args))),    /* with */
  328.             CADDR(CDR(CDR(CDR(args)))));    /* where */
  329.         break;
  330.     case OPERATOR:
  331.         DefineOperator(
  332.                CString(CADR(args)),    /* operator name */
  333.                CDR(CDR(args)));    /* rest */
  334.         break;
  335.     case AGGREGATE:
  336.         DefineAggregate(
  337.                 CString(CADR(args)),/*aggregate name */
  338.                 CDR(CDR(args)));   /* rest */
  339.         break;
  340.  
  341.         case FUNCTION:
  342.         DefineFunction(CDR(args), dest);   /* everything */
  343.         break;
  344.  
  345.         case RULE:
  346.         elog(WARN,
  347.          "Sorry, the old rule system is not supported any more (yet!)");
  348.         break;
  349.     case REWRITE:
  350.         DefineQueryRewrite ( CDR( CDR (args ))) ; 
  351.         break;
  352.     case P_TUPLE:
  353.         prs2DefineTupleRule(args, commandString);
  354.         break;
  355.     case P_TYPE:
  356.         DefineType (CString(CADR(args)),    /* type name */
  357.             CDR(CDR(args)));    /* rest */
  358.         break;
  359.     case VIEW:
  360.         DefineView (CString(CADR(args)),    /* view name */
  361.             CDR(CDR(args)) );    /* retrieve parsetree */
  362.         break;
  363.     default:
  364.         elog(WARN, "unknown object type in define statement");
  365.     } /* switch for define statements */
  366.     break;
  367.     
  368.     case REMOVE:
  369.     commandTag = "REMOVE";
  370.     CHECK_IF_ABORTED();
  371.     
  372.     switch(CInteger(CAR(args))) {
  373.     case FUNCTION:
  374.         RemoveFunction(CString(CADR(args)));
  375.         break;
  376.     case AGGREGATE:
  377.         RemoveAggregate(CString(CADR(args)));
  378.         break;
  379.     case INDEX:
  380.         RemoveIndex(CString(CADR(args)));
  381.         break;
  382.     case OPERATOR:
  383.         {
  384.         String    type2 = NULL;
  385.         
  386.         args = CADR(args);
  387.         if (length(args) == 3) {
  388.             type2 = CString(CADR(CDR(args)));
  389.         }
  390.         RemoveOperator(CString(CAR(args)), CString(CADR(args)),
  391.                    type2);
  392.         }
  393.         break;
  394.     case P_TUPLE:
  395.         prs2RemoveTupleRule(CString(CADR(args)));
  396.         break;
  397.     case REWRITE:
  398.         RemoveRewriteRule(CString(CADR(args)));
  399.         break;
  400.     case P_TYPE:
  401.         RemoveType(CString(CADR(args)));
  402.         break;
  403.     case VIEW:
  404.         RemoveView(CString(CADR(args)));
  405.         break;
  406.     }
  407.     break;
  408.     
  409.     case FORWARD:
  410.     commandTag = "VERSION";
  411.     CHECK_IF_ABORTED();
  412.     
  413.     CreateVersion(CString(CAR(args)), CADR(args));
  414.     break;
  415.     
  416.     case BACKWARD:
  417.     commandTag = "VERSION";
  418.     CHECK_IF_ABORTED();
  419. #ifdef NOTYET
  420.     CreateBackwardDelta( CString(CAR(args)),
  421.                 CString(CADR(args)));
  422.     break;
  423. #endif
  424.     
  425.     case CREATEDB:
  426.     commandTag = "CREATEDB";
  427.     CHECK_IF_ABORTED();
  428.     {
  429.         char *dbname;
  430.         dbname = CString(CAR(args));
  431.         createdb(dbname);
  432.     }
  433.     break;
  434.     case DESTROYDB:
  435.     commandTag = "DESTROYDB";
  436.     CHECK_IF_ABORTED();
  437.     {
  438.         char *dbname;
  439.         dbname = CString(CAR(args));
  440.         destroydb(dbname);
  441.     }
  442.     break;
  443.  
  444.         /* Query-level asynchronous notification */
  445.     case NOTIFY:
  446.     commandTag = "NOTIFY";
  447.     CHECK_IF_ABORTED();
  448.     {
  449.         char *relname;
  450.         relname = CString(rt_relname(nth(CInteger(CAR(args))-1,
  451.                      CAR(CDR(args)))));
  452.         Async_Notify(relname);
  453.     }
  454.         break;
  455.  
  456.     case LISTEN:
  457.     commandTag = "LISTEN";
  458.     CHECK_IF_ABORTED();
  459.     {
  460.         extern int MasterPid;
  461.         char *relname;
  462.         relname = CString(CAR(args));
  463.         Async_Listen(relname,MasterPid);
  464.     }
  465.     break;
  466.  
  467.     /* ********************************
  468.      *    dynamic loader
  469.      * ********************************
  470.      */
  471.     case LOAD:
  472.     commandTag = "LOAD";
  473.     CHECK_IF_ABORTED();
  474.     {
  475.         FILE *fp, *fopen();
  476.         
  477.         char *filename;
  478.         filename = CString(CAR(args));
  479.         if (*filename != '/') {
  480.         elog(WARN, "Use full pathname for LOAD command.");
  481.         }
  482.         
  483.         closeAllVfds();
  484.         if ((fp = fopen(filename, "r")) == NULL) {
  485.         elog(WARN, "LOAD: could not open file %s", filename);
  486.         }
  487.         
  488.         fclose(fp);
  489.         load_file(filename);
  490.     }
  491.     break;
  492.  
  493.     case VACUUM:
  494.     commandTag = "VACUUM";
  495.     CHECK_IF_ABORTED();
  496.     vacuum();
  497.     break;
  498.  
  499.     /* ********************************
  500.      *    default
  501.      * ********************************
  502.      */
  503.     default:
  504.     elog(WARN, "ProcessUtility: command #%d unsupported", command);
  505.     break;
  506.     }
  507.     
  508.     /* ----------------
  509.      *    tell fe/be or whatever that we're done.
  510.      * ----------------
  511.      */
  512.     EndCommand(commandTag, dest);
  513. }
  514.  
  515.